//
//  RegisterViewController.m
//  SmartWatch
//
//  Created by zwj on 2022/11/23.
//

#import "RegisterViewController.h"
#import "AgreementViewController.h"

@interface RegisterViewController ()

@end

@implementation RegisterViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.isHideNavigationBar = YES;
    self.view.backgroundColor = BACKGROUNDCOLOR;
}

- (void)initSubviews {
    [super initSubviews];
    
    UIImageView *backImage = [[UIImageView alloc]initWithImage:UIImageMake(@"profile_back")];
    backImage.frame = CGRectMake(0, 0, kScreenWidth, 220);
    [self.view addSubview:backImage];
    
    UIButton *backBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [backBtn setImage:UIImageMake(@"icon_arrow_left") forState:UIControlStateNormal];
    backBtn.frame = CGRectMake(15, StatusBarHeightConstant, 40, 40);
    backBtn.imageEdgeInsets = UIEdgeInsetsMake(10,10,10,10);
    [backBtn addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:backBtn];
    
    
    MyLinearLayout *root = [MyLinearLayout linearLayoutWithOrientation:MyOrientation_Vert];
    root.myTop = 20 + NavigationContentTop;
    root.myHorzMargin = 15;
    root.subviewVSpace = 15;
    [self.view addSubview:root];
    
    UILabel *name = [UILabel createLabelText:@"注册账号" font:UIFontBoldMake(24) textColor:COLOR1515];
    name.mySize = kAutoSize;
    [root addSubview:name];
    
    MyLinearLayout *content = [MyLinearLayout linearLayoutWithOrientation:MyOrientation_Vert];
    content.myHorzMargin = 0;
    content.myTop = 0;
    content.padding = UIEdgeInsetsMake(0, 15, 0, 15);
    content.backgroundColor = COLOR_4;
    ViewCorner(content, 15);
    [root addSubview:content];
    
    NSArray *array = @[
    @{@"name":@"手机号",@"content":@"请输入手机号"},
    @{@"name":@"验证码",@"content":@"请输入验证码"},
    @{@"name":@"密码",@"content":@"请输入密码"},
    @{@"name":@"确认密码",@"content":@"请再次输入密码"},];
    for (int i = 0; i < array.count; i++) {
        MyLinearLayout *item = [MyLinearLayout linearLayoutWithOrientation:MyOrientation_Horz];
        item.myHorzMargin = 0;
        item.gravity = MyGravity_Vert_Center;
        item.myHeight = 50;
        item.subviewHSpace = 10;
        [content addSubview:item];

        {
            UILabel *name = [UILabel createLabelText:array[i][@"name"] font:UIFontMake(15) textColor:COLOR1515];
            name.mySize = kAutoSize;
            name.myWidth = 100;
            [item addSubview:name];
            
            QMUITextField * tf = QMUITextField.new;
            tf.weight = 1;
            NSMutableAttributedString *attrString = [[NSMutableAttributedString alloc] initWithString:array[i][@"content"] attributes:
                                                         @{NSForegroundColorAttributeName:HexColor(@"#BABABA"),
                                                           NSFontAttributeName:UIFontMake(15)}
            ];
            tf.attributedPlaceholder = attrString;
            tf.myHeight = 30;
            tf.keyboardType = i == 0 ? UIKeyboardTypeNumberPad: UIKeyboardTypeAlphabet;
            tf.maximumTextLength = i ==0 ? 11 : 20;
            tf.secureTextEntry = i == 0 ? NO : YES;
            tf.clearButtonMode = UITextFieldViewModeWhileEditing;
            tf.tag = 100 + i;
            [item addSubview:tf];
            
            if(i == 1){
                UIButton *forgetBtn = [UIButton createButtonText:@"获取验证码" font:UIFontMake(14) textColor:THEME_COLOR];
                forgetBtn.myRight = 0;
                forgetBtn.mySize = kAutoSize;
                [forgetBtn addTarget:self action:@selector(veriBtnClick:) forControlEvents:UIControlEventTouchUpInside];
                [item addSubview:forgetBtn];
                
            }
        }
        
        if(i < array.count - 1) {
            UIView *line = UIView.new;
            line.myHorzMargin = 0;
            line.myHeight = 0.5;
            line.backgroundColor = HexColor(@"#E3E3E3");
            [content addSubview:line];
        }
        if(i == 2){
            UILabel *tishi = [UILabel createLabelText:@"密码至少为6位，其中至少包括一位英文字母和数字" font:UIFontMake(12) textColor:HexColor(@"#454545")];
            tishi.myTop = 10;
            tishi.mySize = kAutoSize;
            [content addSubview:tishi];
            
            UIView *line = UIView.new;
            line.myTop = 30;
            line.myHorzMargin = 0;
            line.myHeight = 0.5;
            line.backgroundColor = HexColor(@"#E3E3E3");
            [content addSubview:line];
        }
        
    }
    
    UIButton *submitBtn = [UIButton createButtonText:@"提交" font:UIFontMake(14) textColor:COLOR_4];
    submitBtn.myHorzMargin = 0;
    submitBtn.myHeight = 46;
    ViewCorner(submitBtn, 23);
    [submitBtn az_setGradientBackgroundWithColors:@[HexColor(@"#4BD8B1"),HexColor(@"#2CAED8")] locations:@[@0.0,@1.0] startPoint:CGPointMake(0, 0.5) endPoint:CGPointMake(1, 0.5)];
    [submitBtn addTarget:self action:@selector(submitBtnClick) forControlEvents:UIControlEventTouchUpInside];
    [root addSubview:submitBtn];
}

- (void)back {
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)veriBtnClick:(UIButton *)sender {
    
    QMUITextField * phone = (QMUITextField *)[self.view viewWithTag:100];
    if(phone.text.length < 11){
        [QMUITips showError:@"请输入正确的手机号"];
        return;
    }
    
    __block NSInteger time = VerificationCodeNumber; //倒计时时间
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    dispatch_source_t timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, queue);
    dispatch_source_set_timer(timer,DISPATCH_TIME_NOW,1.0*NSEC_PER_SEC, 0); //每秒执行
    dispatch_source_set_event_handler(timer, ^{
        if(time <= 0){ //倒计时结束，关闭
            dispatch_source_cancel(timer);
            dispatch_async(dispatch_get_main_queue(), ^{
                sender.enabled = YES;
                [sender setTitle:@"获取验证码" forState:0];
            });
        }else{
            dispatch_async(dispatch_get_main_queue(), ^{
                sender.enabled = NO;
                [sender setTitle:FormatString(@"%lds后获取",time) forState:0];
                time--;
            });
        }
    });
    dispatch_resume(timer);
    
}

- (void)submitBtnClick {
    
    
    [self.view endEditing:YES];
    
//    QMUITextField * phone = (QMUITextField *)[self.view viewWithTag:100];
//    if(phone.text.length < 11){
//        [QMUITips showError:@"请输入正确的手机号"];
//        return;
//    }
//    QMUITextField * code = (QMUITextField *)[self.view viewWithTag:101];
//    if(code.text.length < 4){
//        [QMUITips showError:@"请输入正确的验证码"];
//        return;
//    }
//
//    QMUITextField * pass = (QMUITextField *)[self.view viewWithTag:102];
//    if(![ZJCommon checkPassword:pass.text]){
//        [QMUITips showError:@"密码至少包括一位英文和数字"];
//        return;
//    }
//    QMUITextField * pass2 = (QMUITextField *)[self.view viewWithTag:103];
//
//    if(![pass.text isEqualToString:pass2.text]){
//        [QMUITips showError:@"2次输入的密码必须一致"];
//        return;
//    }
    
//    NSMutableDictionary *mutDic = [NSMutableDictionary dictionary];
//    mutDic[@"phone"] = phone.text;
//    mutDic[@"code"] = code.text;
//    mutDic[@"pass"] = pass.text;
    
    
    AgreementViewController *vc = AgreementViewController.new;
    [self.navigationController pushViewController:vc animated:YES];
}
@end
